package com.kunteng.ynhxksh.service;

/**
 * Created By ShiYun with IntelliJ IDEA on 2018/4/2 10:47
 *
 * @author: ShiYun
 * @Email: 515867726@qq.com
 * @Date: 2018/4/2
 * To change this template use File | Settings | File Templates.
 */

import com.kunteng.ynhxksh.dao.KPGKDao;
import com.kunteng.ynhxksh.mapper.D_T_DZFP_FPJBXXMapper;
import com.kunteng.ynhxksh.util.DateUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.*;

/**
 * 开票管控服务
 */
@Service
public class KPGKService implements KPGKDao {

    Logger logger = LoggerFactory.getLogger(KPGKService.class);
    @Autowired
    D_T_DZFP_FPJBXXMapper d_t_dzfp_fpjbxxMapper;

    DecimalFormat df = new DecimalFormat("#########0.0000");

    @Override
    public List<Map> mcRate() {
        List<Map> mcRates = new ArrayList<>();
        List<Map> mc5 = d_t_dzfp_fpjbxxMapper.selectMCRate();
        BigDecimal JETotal = d_t_dzfp_fpjbxxMapper.selectJETotal();
        BigDecimal sumje = new BigDecimal(0.00);
        double sumRate = 0.0000;
        for (Map map : mc5) {
            BigDecimal je = (BigDecimal) map.get("je");
            double rate = Double.parseDouble(df.format(je.doubleValue() / JETotal.doubleValue()));
            map.put("rate", rate);
            mcRates.add(map);
            sumje = sumje.add(je);
            sumRate = sumRate + rate;
        }
        Map other = new HashMap<>();
        other.put("je", JETotal.subtract(sumje));
        other.put("rate", 1 - sumRate);
        other.put("mc", "其他");
        mcRates.add(other);
        System.out.println("mcRates.size()=  " + mcRates.size());
        return mcRates;
    }

    @Override
    public List<Map> timeInfoKP() {
        return d_t_dzfp_fpjbxxMapper.getTimeInfoKP();
    }

    @Override
    public JSONObject queryKPYC() {

        String dateStr = DateUtils.getDate_yyyy_MM_dd(new Date());

        logger.info("queryKPYC {}", dateStr);


        Map<String, Integer> map = new HashMap();

        map.put("0", 0);
        map.put("1", 0);
        map.put("2", 0);
        map.put("3", 0);
        map.put("4", 0);
        map.put("5", 0);
        map.put("6", 0);
        map.put("7", 0);

        List<Map> list = d_t_dzfp_fpjbxxMapper.queryKPYCByDate(dateStr);

        for (Map one : list) {

            int ycdays = ((Long) one.get("ycdays")).intValue();
            int num = ((Long) one.get("num")).intValue();
            if (ycdays < 7) {
                map.put(String.valueOf(ycdays), num);
            } else {
                int total = map.get("7") + num;
                map.put("7", total);
            }
        }
        JSONObject jsonObject = new JSONObject();
        for (int i = 0; i < 8; i++) {
            jsonObject.put(i, map.get(String.valueOf(i)));
        }
        return jsonObject;
    }

}
